package com.datasentinel.rooms.ui.sh3d;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;

import com.datasentinel.rooms.ui.sh3d.CustomHomeComponent3D.ActionType;
import com.datasentinel.util.ui.gesture.ActivationGestureEvent;

public class RightClickMenu implements ActionListener
{
	private static Logger logger = Logger.getLogger(RightClickMenu.class.getName());
	private Font font = new Font("Arial", Font.PLAIN,  11);
	private CustomHomeComponent3D home3D;
	public RightClickMenu(CustomHomeComponent3D home3D)
	{
		this.home3D = home3D;
	}
	public JPopupMenu buildMenu()
	{
		JPopupMenu topMenu = new JPopupMenu();
		topMenu.setLightWeightPopupEnabled(false);

		JMenuItem menuItem;
		for (int i = 0; i < MAINMENUITEMS.length; i++)
		{	
			if (MAINMENUITEMS[i].length == 1)
			{
				topMenu.add(menuItem = new JMenuItem(MAINMENUITEMS[i][0][0]));
				menuItem.addActionListener(this);
				menuItem.setFont(font);
				if (menuItem.getText().equals(WEB_LINK) && home3D.getHomeName().indexOf("(private)") >= 0)
					menuItem.setEnabled(false);
				continue;
			}
			JMenu menu = new JMenu(MAINMENUITEMS[i][0][0]);
			menu.getPopupMenu().setLightWeightPopupEnabled(false);
			menu.setFont(font);
			menu.setMnemonic(Character.toLowerCase(MAINMENUITEMS[i][0][0].charAt(0)));
			for (int  j = 1; j < MAINMENUITEMS[i].length; j ++)
			{
				menuItem = new JMenuItem(MAINMENUITEMS[i][j][0]);
				menuItem.addActionListener(this);
				menuItem.setFont(font);
				
				if (MAINMENUITEMS[i][j].length > 2)
						menuItem.setAccelerator(KeyStroke.getKeyStroke(MAINMENUITEMS[i][j][2]));
				if (MAINMENUITEMS[i][j][1] != null)
					menuItem.setMnemonic(MAINMENUITEMS[i][j][1].charAt(0));
				menu.add(menuItem);
				menuItem.setFont(font);
			}
			topMenu.add(menu);
		}
		return topMenu;
	}
	private static String WEB_LINK = "Copy web link";
	private static String MAINMENUITEMS[][][] = {
		  /*00*/{{"Visit Furniture"}, {"Living Room", "l"}, {"Bedroom", "b"}, {"Kitchen", "k"}, {"Bathroom", "t"}, {"Doors, Windows", "d"}, {"Miscellaneous", "m"}},
		  /*10*/{{"Visit Friends", "f",}},
		  /*20*/{{"Edit"}, {"Cut", "t", "control X"}, {"Copy", "c", "control  C"}, {"Paste", "p", "control V"}, {"Select All", "a", "control  A"}, {"Delete", "d", "delete"}},
		  /*30*/{{"New Room"}, {"Public", "u"}, {"Private", "r"}},
		  /*40*/{{"New Banner", "b"}},
		  /*50*/{{WEB_LINK}},
		  /*60*/{{"Go home", "h"}},
		  /*70*/{{"Help", "?"}}}; 
	
	@Override
	public void actionPerformed(ActionEvent e)
	{
		String actionCommand = e.getActionCommand();
		for (int i = 0; i < MAINMENUITEMS.length; i++)
			for (int j = 0; j < MAINMENUITEMS[i].length; j++)
				if (actionCommand.startsWith(MAINMENUITEMS[i][j][0]))
					switch ((i*10) + j)
					{
						case 1:
						case 2:
						case 3:
						case 4:
						case 5:
						case 6: 
						case 60: fireGesture(e, "goto", actionCommand);
						break;
						case 10: fireGesture(e, "visit", actionCommand);			break;
						case 21: fireKeyAction(ActionType.CUT_OPERATION, e); 		break;
						case 22: fireKeyAction(ActionType.COPY_OPERATION, e); 		break;
						case 23: fireKeyAction(ActionType.PASTE_OPERATION, e); 		break;
						case 24: fireKeyAction(ActionType.SELECTALL_OPERATION, e); 	break;
						case 25: fireKeyAction(ActionType.DELETE_OPERATION, e); 	break;
						case 31:
						case 32: fireGesture(e, "new", actionCommand);				break;
						case 40: fireKeyAction(ActionType.NEW_BANNER, e); 			break;
						case 50: fireGesture(e, "weblink", actionCommand);			break;
						case 70: fireGesture(e, "help", actionCommand);				break;
					}
	}
	private void fireGesture(ActionEvent e, String function, String actionCommand)
	{
		logger.log(Level.INFO, "Menu: " + function + ":" + actionCommand);
		home3D.fireGestureRecognizedEvent(new ActivationGestureEvent(e.getSource(), function + ":" + actionCommand, null));
	}

	private void fireKeyAction(ActionType actionType, ActionEvent e)
	{
		ActionListener listener = home3D.getActionMap().get(actionType);
		if (listener != null)
			listener.actionPerformed(e);
	}
}
